package cn.tianya.irock.security;

import cn.tianya.irock.data.dalfactory.DataAccess;
import cn.tianya.irock.data.idal.IPermissionDataProvider;
import cn.tianya.irock.data.model.Post;
import cn.tianya.irock.data.model.Section;
import cn.tianya.irock.data.model.User;
import cn.tianya.irock.enums.ApplicationExceptionType;
import cn.tianya.irock.enums.ApplicationType;
import cn.tianya.irock.exceptions.ApplicationException;

public class Permissions {

	private static IPermissionDataProvider provider;

    static {
        provider = DataAccess.createPermissionDataProvider();
    }
    
	//public static Permission getPermission(int groupId, int roleId) {
		//List<Permission> permissions = provider.getPermissions(groupId);
//
		//for(Permission pf:permissions)
		//{
			//if (pf.getRoleId() == roleId)
			//	return pf;
		//}
	//	return null;
	//}
    
   // public static void update (PermissionBase pb) {
    //    validate(pb);
    //    provider.update(pb, DataProviderAction.Update);
    //}

    public static void remove (PermissionBase p) {

    }

    public static void add (PermissionBase p) {
    	
    }
    
    private static void validate(PermissionBase p) {
        if(p.getApplicationType() == ApplicationType.Unknown)
            throw new ApplicationException(ApplicationExceptionType.PermissionApplicationUnknown,"Permission has an Unknown ApplicationType");
    }
    
    public static boolean validatePermissions(Section section, long permission, User user) {
        return validatePermissions(section, permission, user, null);
    }
    
    public static boolean validatePermissions(Section section, long permission, User user, Post p) {
        return section.validate(permission, user, p);
    }
    
    public static void accessCheck (Section section, long permission, User user, Post post) {
    	if(section == null)
            throw new ApplicationException(ApplicationExceptionType.SectionNotFound, "Unknown Section Requested");

        section.accessCheck(permission, user, post);
    } 
}
